'use strict';
// 云函数news.js（无需修改，已正确处理参数）
exports.main = async (event, context) => {
  const db = uniCloud.database();
  const collection = db.collection('news');
  
  try {
    let query = {};
    
    // 根据分类ID筛选（event.categoryId由客户端传递）
    if (event.categoryId && event.categoryId !== 'all') {
      query.category_id = event.categoryId;
    }
    
    const res = await collection.where(query).get();
    return res;
  } catch (e) {
    console.error('查询新闻失败', e);
    return {
      code: 500,
      message: '查询失败',
      error: e
    };
  }
};